#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<ctype.h>

#define  TMP_FILE    "/tmp/.ifconfig.log"


int main(int argc, char* argv[])
{

	pid_t        pid;
	int          fd;
	char         buf[1024];
        char         *ptr;
	FILE         *fp;
	int          rv;
	char         *ip_start;
	char         *ip_end;
	char         ipaddr[16];




	if((fd=open(TMP_FILE,O_RDWR|O_CREAT|O_TRUNC,0644)<0))
	
	{
		printf("redirect standard output to file failure:%s",strerror(errno));
		return -1;
	}
	pid=fork();
	if(pid<0)
	{
	
		printf("fork() create  failure:%s\n",strerror(errno));
		return -2;
	}
	else if(pid==0)
	{
	
		printf("child process start excute\n");
		dup2(fd,STDOUT_FILENO);
		execl("/sbin/ifconfig","ifconfig","eth0","NULL");
		printf("return means execl() error\n");
			return -1;
	}
	else
	{
	
		sleep(3);

	}
	memset(buf,0,sizeof(buf));
//	lseek(fd,0,SEEK_SET);
//	rv=read(fd,buf,sizeof(buf));
        fp=fdopen(fd,"r");
	fseek(fp,0,SEEK_SET);
	while(fgets(buf,sizeof(buf),fp))
	{

		if(strstr(buf,"netmask"))
		{
            ptr=strstr(buf,"inet");
	    if(!ptr)
	    {

	          break;	    

	    }
	    ptr+=strlen("inet");
	    while(isblank(*ptr))
		    ptr++;
	    ip_start=ptr;
	    while(!isblank(*ptr))
		    ptr++;
	    ip_end=ptr;

	    memset(ipaddr,0,sizeof(ipaddr));
	    memset(ipaddr,0,ip_end-ip_start);
	    break;

         	}


        }
           printf("parser and get ip address:%s\n",ipaddr);
           fclose(fp);
           unlink(TMP_FILE);
           return 0;
 }

